package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.domain.TravelContent;
import cn.wolfcode.luowowo.article.mapper.TravelContentMapper;
import cn.wolfcode.luowowo.article.mapper.TravelMapper;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.common.query.TravelQueryObject;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class TravelServiceImpl implements ITravelService{
    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private TravelContentMapper travelContentMapper;

    public PageInfo query(TravelQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Travel> list = travelMapper.selectForList(qo);
        return new PageInfo(list);
    }

    public void deleteById(Long id) {
        travelMapper.deleteByPrimaryKey(id);
    }


    public Long saveOrUpdate(Travel travel) {
        //文章摘要
        String content = travel.getTravelContent().getContent();
        if(content.length() >200 ){
            travel.setSummary(content.substring(0,200));
        }else{
            travel.setSummary(content);
        }
        //设置最后更新时间
        travel.setLastUpdateTime(new Date());
        if(travel.getId() == null){
            //新增操作
            //创建时间
            travel.setCreateTime(new Date());
            travelMapper.insert(travel);
            //保存文章内容
            Long id = travel.getId();
            TravelContent travelContent = travel.getTravelContent();
            travelContent.setId(id);
            travelContentMapper.insert(travelContent);
        }else{
            //更新操作
            travelMapper.updateByPrimaryKey(travel);
            //更新文章内容
            Long id = travel.getId();
            TravelContent travelContent = travel.getTravelContent();
            travelContent.setId(id);
            travelContentMapper.updateById(travelContent);
        }
        return travel.getId();
    }

    public List<Travel> selectAll() {

        return travelMapper.selectAll();
    }

    public Travel getById(Long id) {
        return travelMapper.selectByPrimaryKey(id);
    }

    public TravelContent getContent(Long id) {
        return travelMapper.getContent(id);
    }

    public List<Travel> getTravelTop3(Long id) {
        return travelMapper.getTravelTop3(id);
    }

    public List<Travel> selectByAuthorId(Long authorId) {
        List<Travel> travels = travelMapper.selectByAuthorId(authorId);
        for (Travel t :travels) {

            TravelContent content = travelContentMapper.selectByAuthorId(t.getId());
            t.setTravelContent(content);
        }

        return travels;

    }



    public int queryCountByAuthorId(Long authorId) {
        return travelMapper.selectCountByAuthorId(authorId);
    }

    public List<StrategyDetail> listAll() {
        
        return null;
    }


}
